package siafi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import siafi.bean.Concepto;
import siafi.bean.PersonaCentro;
import siafi.bean.Servicio;
import siafi.db.TerceroDAO;

/**
 * Servlet implementation class ResumenRecibo
 */
public class ResumenRecibo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResumenRecibo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		@SuppressWarnings("unchecked")
		Enumeration<String> nombres = request.getParameterNames();
		List <String> serviciosS = new ArrayList<String>();
		String ntemp;
		
		Boolean aut = false;
		if ((Boolean) request.getSession().getAttribute("aut") != null && (Boolean) request.getSession().getAttribute("aut")){
			aut = true;			
		}
		Boolean autEst = false;
		if ((Boolean) request.getSession().getAttribute("autEst") != null && (Boolean) request.getSession().getAttribute("autEst")){
			autEst = true;			
		}
		if (!aut && !autEst){
			response.sendRedirect("index.xhtml");
		} else{
		
		while(nombres.hasMoreElements()){
			ntemp = nombres.nextElement();			
			if(ntemp.indexOf("servicio-") != -1)
				serviciosS.add(ntemp.substring(ntemp.indexOf("-")+1));
		}
		List<Servicio> servicios = (List<Servicio>)request.getSession().getAttribute("servicios");
		//List<Servicio> servicios = new ArrayList<Servicio>();
		///CHECKBOX
		String[] conceptosElegidos = request.getParameterValues("conceptos");
		List<Servicio> servicioCB = new ArrayList<Servicio>();
		if (conceptosElegidos.length>0){
			for (String c : conceptosElegidos){
				for (Servicio s1 : servicios){
					if (c.equals(s1.getConcepto())){
						servicioCB.add(s1);
					}
				}
				System.out.println();
			}			
		}
		servicios = new ArrayList<Servicio>();
		if (servicioCB.size()>0){
			servicios =	servicioCB;		
		}
		///
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
		out.println("<html>");
		out.println(" <head>");
		out.println(" <link href=\"./css/coolblue.css\" rel=\"stylesheet\" type=\"text/css\"/>");
		out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
		out.println("  <title>Recibos</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("   <div align=\"center\" id=\"header\">");
		out.println(" <img src=\"./images/upn.jpg\"/>");
		out.println(" </div>");
		float total = 0;
		if( request.getSession().getAttribute("tercero")!=null){
		if (!servicios.isEmpty()) {
			out.println(" <div align=\"center\" id=\"content\">");
			out.println(" <form action=\"/recibosSIAFI/GenerarReciboServicio\" method=\"post\">");
			out.println("     <table>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\"><h3>Recibo de Servicio</h3></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\" align=\"left\"><h4 align=\"left\">"+((PersonaCentro)request.getSession().getAttribute("tercero")).getNombre()+"</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\" align=\"left\"><h4>Servicio a pagar</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <th>Servicio");
			out.println(" </th>");

			out.println(" <th>Valor Unitario");
			out.println(" </th>");

			out.println(" <th>Cantidad");
			out.println(" </th>");

			out.println(" </tr>");
			for(Servicio s : servicios){
				s.setValor_unitario(Float.parseFloat(request.getParameter("valor-"+s.getCod_concepto())));
				s.setCan_liquidacion(Integer.parseInt(request.getParameter("servicio-"+s.getCod_concepto())));
			out.println(" <tr>");
			out.println(" <td><label>"+s.getConcepto()+"</label></td>");
			out.println(" <td> <input type=\"text\" size=\"10\" name=\"valor-"+s.getCod_concepto()+"\" readonly=\"readonly\" value=\""+s.getValor_unitario()+"\"> </td>");
			out.println(" <td> ");
			out.println("  <input type=\"text\" size=\"3\" name=\"servicio-"+s.getCod_concepto()+"\" value=\""+request.getParameter("servicio-"+s.getCod_concepto())+"\" readonly=\"readonly\"> ");
			out.println("</td>");
			out.println("</tr>");
			total += s.getValor_unitario()*(float)s.getCan_liquidacion();
			}
			request.getSession().setAttribute("valor_total", Math.round(total));
			request.getSession().setAttribute("servicios", servicios);
			out.println(" <tr>");
			out.println(" <td colspan=\"2\" align=\"right\"><h4>Total: "+Math.round(total)+"</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			
			if (servicios.size()<8)
				out.println("<td colspan=\"2\" align=\"right\"><button type=\"submit\" class=\"button\"><img src=\"images/btn_generar.gif\"/></button></td>");
			else
				out.println("<td><h4>POR FAVOR SELECCIONE MAXIMO 7 SERVICIOS POR RECIBO</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td><a href=\"indexServicio.jsp\"><img src=\"images/btn_vINICIO.gif\"/> </a></td>");
			out.println(" <td><button type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\"><img src=\"images/btn_regresar.gif\"/></button></td>");
			
			out.println("<td><a href=\"Logout\"><img alt=\"salir\" src=\"images/btn_terminar.gif\"/> </a></td>");
			
			out.println(" </tr>");
			out.println("</table>");
			out.println("</form>");
			out.println("</div>");
		}
		}
		out.println(" <div align=\"right\" id=\"footer\" style=\"width: 80%\">");
		out.println("<img src=\"./images/SiafiReportes.bmp\" width=\"100px\" height=\"30px\"/>");
		out.println("</div>");

		out.println("</body>");
		out.println("</html>");

		out.close();
	}
	}
}
